package com.live555.utils;

import com.coremedia.iso.boxes.Container;
import com.googlecode.mp4parser.FileDataSourceImpl;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.tracks.H264TrackImpl;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * 功能描述： 将标准的H264件转换成mp4文件
 * -----------------------------------------
 * 实现的思路:
 * 1、 从服务器接收H264码流, 将解码后的数据保存到本地 xx.h264
 * 2、将xx.h264转换成xx.mp4
 *
 * @author：ZC 创建时间：2017/5/31 13:52
 * 最后修改日期：
 */

public class H264ToMp4Utils {

    /**
     * @param h264Path h264文件绝对路径
     * @param mp4Path  mp4绝对路径
     * @return 是否转码成功
     */
    public static boolean h264ToMp4Parse(String h264Path, String mp4Path) {
//        LogUtils.i("h264ToMp4Parse 转码  h264Path=", "" + h264Path + "\r\n" + mp4Path);
        try {
            H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(h264Path));
            Movie movie = new Movie();
            movie.addTrack(h264Track);

            Container mp4file = new DefaultMp4Builder().build(movie);
            FileChannel fc = new FileOutputStream(new File(mp4Path)).getChannel();
            mp4file.writeContainer(fc);
            fc.close();
            return true;
        } catch (Exception exception) {
//            LogUtils.i("h264ToMp4Parse 转码异常，耗时=", "" + exception.getMessage());
            return false;
        }
    }
}
